#######################################################################################################################
import sys
import os


#######################################################################################################################
if len(sys.argv) != 4:
	print 'use: conv_level.py level.raw level.dat tilemask.txt'
	sys.exit(-1)

raw_name = sys.argv[1]
data_name = sys.argv[2]
tile_mask = sys.argv[3]

raw_file = file(raw_name, 'rt')
raw_str = raw_file.read()
raw_file.close()

lines = raw_str.split()
chars = {}
for line in lines:
	for char in line:
		chars[char] = 0
idx = 0
for char in chars.keys():
	chars[char] = idx
	tile_name = tile_mask % idx
	idx += 1
	tile_file = file(tile_name, 'wt')
	tile_file.write(char+'\n')
	tile_file.close()

data_file = file(data_name, 'wt')
for line in lines:
	for char in line:
		data_file.write('%d,'%chars[char])
	data_file.write('\n')
data_file.close()
